#include <bits/stdc++.h>

using namespace std;
class Solution
{
public:
    vector<bool> checkIfPrerequisite(int numCourses, vector<vector<int>> &prerequisites, vector<vector<int>> &queries)
    {
        vector<vector<int>> adj(numCourses, vector<int>(numCourses, 0));
        for (int i = 0; i < numCourses; i++)
        {
            for (int j = 0; j < numCourses; j++)
            {
                if (i == j)
                {
                    continue;
                }
                adj[i][j] = INT_MAX / 2;
            }
        }
        for (auto e : prerequisites)
        {
            adj[e[0]][e[1]] = 1;
        }

        for (int k = 0; k < numCourses; k++)
        {
            for (int i = 0; i < numCourses; i++)
            {
                for (int j = 0; j < numCourses; j++)
                {
                    adj[i][j] = min(adj[i][j], adj[i][k] + adj[k][j]);
                }
            }
        }

        vector<bool> ans(queries.size(), false);
        for (int i = 0; i < queries.size(); i++)
        {
            if (adj[queries[i][0]][queries[i][1]] != INT_MAX / 2)
            {
                ans[i] = true;
            }
        }
        return ans;
    }
};